package cn.fan.testfunction.web;

import cn.fan.testfunction.even.PublishEvent;
import cn.fan.testfunction.model.VaildTest;
import cn.fan.testfunction.result.ResponseResult;
import cn.fan.testfunction.service.RetryService;
import cn.fan.testfunction.service.TestService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;

/**
 * TODO
 *
 * @author HTHLKJ
 * @version 1.0
 * @date 2021/4/16 13:46
 */
@RestController
public class TestColntroller {

   final static Semaphore semaphore = new Semaphore(5);
   @Autowired
   private RetryService retryService;

   @Autowired
   private PublishEvent publishEvent;

    /**
     * requestBody 参数校验
     * 如果校验失败，会抛出MethodArgumentNotValidException异常，Spring默认会将其转为400（Bad Request）请求。
     * @param test
     */
    @PostMapping("/test/vaild")
    public ResponseResult testVaild(@Valid @RequestBody VaildTest test) {
//StringUtils.isEmpty
        System.out.println(StringUtils.isEmpty(""));  //true
        System.out.println(StringUtils.isEmpty("  "));  //false
//StringUtils.isNotEmpty
        System.out.println(StringUtils.isNotEmpty(""));  //false

//StringUtils.isBlank
        System.out.println(StringUtils.isBlank(""));  //true
        System.out.println(StringUtils.isBlank(" "));  //true
//StringUtils.isNotBlank
        System.out.println(StringUtils.isNotBlank(" "));  //false

        List<Integer> emptyList = new ArrayList<>();
        List<Integer> nullList = null;
        List<Integer> notEmptyList = new ArrayList<>();
        notEmptyList.add(1);

//CollectionUtils.isEmpty
        System.out.println(CollectionUtils.isEmpty(emptyList));   //true
        System.out.println(CollectionUtils.isEmpty(nullList));   //true
        System.out.println(CollectionUtils.isEmpty(notEmptyList));   //false

////CollectionUtils.isNotEmpty
//        System.out.println(CollectionUtils.isNotEmpty(emptyList));   //false
//        System.out.println(CollectionUtils.isNotEmpty(nullList));   //false
//        System.out.println(CollectionUtils.isNotEmpty(notEmptyList));   //true
        return ResponseResult.SUCCESS();
    }


    /**
     * requestParam/PathVariable参数校验
     * 如果校验失败，会抛出ConstraintViolationException异常。
     * @param userId
     */
    @GetMapping("{userId}")
    public void detail(@PathVariable("userId") @Min(10000000000000000L) Long userId) {
        // 校验通过，才会执行业务逻辑处理
//        UserDTO userDTO = new UserDTO();
//        userDTO.setUserId(userId);
//        userDTO.setAccount("11111111111111111");
//        userDTO.setUserName("xixi");
//        userDTO.setAccount("11111111111111111");
//        return Result.ok(userDTO);
    }

    // 查询参数
    @GetMapping("getByAccount")
    public void getByAccount(@Length(min = 6, max = 20) @NotNull String  account) {
        // 校验通过，才会执行业务逻辑处理
//        UserDTO userDTO = new UserDTO();
//        userDTO.setUserId(10000000000000003L);
//        userDTO.setAccount(account);
//        userDTO.setUserName("xixi");
//        userDTO.setAccount("11111111111111111");
//        return Result.ok(userDTO);
    }


    /**
     * 信号量限流测试
     */
    @GetMapping("/limit")
    public void limitRate() throws InterruptedException {
        if (semaphore.tryAcquire()){
            System.out.println("没限流啦");
            Thread.sleep(3000);
        }else {
            System.out.println("限流啦");
        }
        semaphore.release();
    }

    @GetMapping("/retry")
    public void retry(){
        retryService.call("good");
    }

    @RequestMapping("/pub")
    public void pub() {
        for (int i = 0; i < 5; i++) {
            publishEvent.publish("你若为我繁华，你好呀：" + (i + 1));
        }
    }
}
